package com.material.controller;

import com.github.pagehelper.PageInfo;
import com.material.common.annotation.LoginUser;
import com.material.common.core.domain.entity.SysUser;
import com.material.common.entity.ResultResponse;
import com.material.common.utils.bean.BeanUtil;
import com.material.pound.FuelPoundParams;
import com.material.pound.service.IFuelPoundService;
import com.material.pound.vo.FuelDiaryVo;
import com.material.request.SupplierDiaryRequest;
import com.material.response.SupplierDiaryCarResponse;
import com.material.response.SupplierDiaryDateResponse;
import com.material.response.SupplierDiaryFuelResponse;
import com.material.response.SupplierDiarySupplierResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ganxinge
 */
@Api(tags = "11.送料日报")
@RestController
@RequestMapping("/supplier/diary")
public class SupplierDiaryController {

    @Autowired
    private IFuelPoundService fuelPoundService;

    @ApiOperation("日报列表（按日期）")
    @GetMapping("/listByDate")
    public ResultResponse<PageInfo<FuelDiaryVo>> listByDate(@ApiIgnore @LoginUser SysUser sysUser, SupplierDiaryRequest supplierDiaryRequest){
        FuelPoundParams fuelPoundParams = BeanUtil.copyProperties(supplierDiaryRequest, FuelPoundParams.class);
        setUserInfo(sysUser,fuelPoundParams);
        PageInfo<FuelDiaryVo> pageInfo = fuelPoundService.selectFuelPoundByDate(fuelPoundParams);
        return ResultResponse.success(pageInfo);
    }

    @ApiOperation("日报列表（按品名）")
    @GetMapping("/listByFuel")
    public ResultResponse<List<SupplierDiaryFuelResponse>> listByFuel(@ApiIgnore @LoginUser SysUser sysUser, SupplierDiaryRequest supplierDiaryRequest){
        FuelPoundParams fuelPoundParams = BeanUtil.copyProperties(supplierDiaryRequest, FuelPoundParams.class);
        setUserInfo(sysUser,fuelPoundParams);
        PageInfo<FuelDiaryVo> pageInfo = fuelPoundService.selectFuelPoundByFuel(fuelPoundParams);
        return ResultResponse.success(pageInfo);
    }

    @ApiOperation("日报列表（按车辆）")
    @GetMapping("/listByCar")
    public ResultResponse<List<SupplierDiaryCarResponse>> listByCar(@ApiIgnore @LoginUser SysUser sysUser, SupplierDiaryRequest supplierDiaryRequest){
        FuelPoundParams fuelPoundParams = BeanUtil.copyProperties(supplierDiaryRequest, FuelPoundParams.class);
        setUserInfo(sysUser,fuelPoundParams);
        PageInfo<FuelDiaryVo> pageInfo = fuelPoundService.selectFuelPoundByCar(fuelPoundParams);
        return ResultResponse.success(pageInfo);
    }

    @ApiOperation("日报列表（按供应商）")
    @GetMapping("/listBySupplier")
    public ResultResponse<List<SupplierDiarySupplierResponse>> listBySupplier(@ApiIgnore @LoginUser SysUser sysUser, SupplierDiaryRequest supplierDiaryRequest){
        FuelPoundParams fuelPoundParams = BeanUtil.copyProperties(supplierDiaryRequest, FuelPoundParams.class);
        setUserInfo(sysUser,fuelPoundParams);
        PageInfo<FuelDiaryVo> pageInfo = fuelPoundService.selectFuelPoundBySupplier(fuelPoundParams);
        return ResultResponse.success(pageInfo);
    }

    private void setUserInfo(SysUser sysUser,FuelPoundParams fuelPoundParams){
        String userType = sysUser.getUserType();
        if(SysUser.USER_TYPE_3.equals(userType)){
            fuelPoundParams.setSupplierId(sysUser.getBizId());
        }else{
            fuelPoundParams.setFactoryId(sysUser.getBizId());
        }
    }
}
